package fr.emse.tatiana.replayable.analysis.graph;

import org.eclipse.draw2d.*;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.text.FlowPage;
import org.eclipse.draw2d.text.ParagraphTextLayout;
import org.eclipse.draw2d.text.TextFlow;
import org.eclipse.swt.graphics.Color;

import fr.emse.tatiana.graphical.IRuleableFigure;

public class EventGraphFigure extends Figure implements IRuleableFigure {

	private TextFlow label;
	private FlowPage flow; 
	private Shape shape;
	private Color bg = ColorConstants.white;

	public EventGraphFigure() {
		
		
			// text label

//			
//	
//			roundedFigure = new RoundedRectangle();
//			roundedFigure.setCornerDimensions(new Dimension(10, 10));
//			roundedFigure.setOpaque(true);
//			roundedFigure.setBackgroundColor(getGraphicalItemModel().getColor());
//			roundedFigure.setLayoutManager(new BorderLayout());
//			roundedFigure.add(tagLabel, BorderLayout.TOP);
//			
//			FlowPage flowPage = new FlowPage();
//			PageFlowLayout pfl = new PageFlowLayout(flowPage);
//			pfl.setContinueOnSameLine(false);
//			
//			flowPage.setOpaque(true);
//			flowPage.setBackgroundColor(ColorConstants.white);
//			flowPage.setHorizontalAligment(PositionConstants.LEFT);
//			flowPage.setOrientation(SWT.LEFT_TO_RIGHT);
//			flowPage.add(label);
//
//			roundedFigure.add(flowPage, BorderLayout.CENTER);
//			
//			// if border property is setted
//			if(getGraphicalItemModel().isBorder()){
//				roundedFigure.setOutline(true);
//				flowPage.setBorder(new CompoundBorder(new LineBorder(), new MarginBorder(5)));
//			} else {
//				roundedFigure.setOutline(false);
//				flowPage.setBorder(new MarginBorder(5));
//			}
//			
//			// maximum size
//			roundedFigure.setPreferredSize(150, 70);
		
		this.setLayoutManager(new BorderLayout());
		
		label = new TextFlow();
		flow = new FlowPage();
		Shape shape = new RectangleFigure();

//		if(getGraphicalItemModel().getLabelText().length() > 0){
//		textLabel.setToolTip(new Label(getGraphicalItemModel().getLabelText()));
//		}

		ParagraphTextLayout ptl = new ParagraphTextLayout(label, ParagraphTextLayout.WORD_WRAP_SOFT);
		label.setLayoutManager(ptl);
		label.setForegroundColor(ColorConstants.black);

		flow.add(label, ToolbarLayout.ALIGN_CENTER);
		//add(flow);
		flow.setOpaque(false);
		flow.setHorizontalAligment(PositionConstants.CENTER);
		//flowPage.setBackgroundColor(ColorConstants.white);
		//flowPage.setHorizontalAligment(PositionConstants.LEFT);
		//flowPage.setOrientation(SWT.LEFT_TO_RIGHT);
		setOpaque(true);
		setShape(shape);
	}

	public void setShape(Shape s) {
		if (this.shape != null) {
			shape.remove(flow);
			this.remove(shape);
		}
		if (s==null) {
			s = new RectangleFigure();
		}
		shape = s;
		shape.setOpaque(true);
		shape.setBackgroundColor(bg);
		shape.setOutline(true);
		shape.setLayoutManager(new BorderLayout());
		shape.add(flow, BorderLayout.CENTER);
		add(shape,BorderLayout.CENTER);
	}
	
	public void setText(String text) {
		label.setText(text);
	}

	public void setLayout(Rectangle rect) {
		getParent().setConstraint(this, rect);
		centreLabel();
	}

	public void centreLabel() {
		// TODO get correct height
		int labelheight = label.getBounds().height;
		int shapeheight = this.getBounds().height;
		int extra = Math.max(0, shapeheight-labelheight);
		flow.setBorder(new MarginBorder(extra/2,4,extra/2,4)); //top left bottom right
	}

	public void moveY(int i) {
		// does nothing

	}

	public void setAbsoluteY(int i) {
		// does nothing

	}

	public void setBorderColor(Color c) {
		this.setForegroundColor(c);

	}

	@Override
	public void setBackgroundColor(Color c) {
		bg = c;
		shape.setBackgroundColor(c);
	}

	public void setHeight(int i) {
		// does nothing

	}


	
	

}
